package com.mark.web.bill.controller;

import com.github.pagehelper.PageInfo;
import com.mark.common.CurrentUser;
import com.mark.common.ServerResponse;
import com.mark.web.bill.entity.from.AddAccountFrom;
import com.mark.web.bill.entity.from.AddAccountNotGoodsFrom;
import com.mark.web.bill.entity.from.EditAccountNotGoodsFrom;
import com.mark.web.bill.entity.po.Account;
import com.mark.web.bill.entity.vo.AccountVo;
import com.mark.web.bill.service.IAccountService;
import com.mark.web.common.entity.vo.echarts.LineChartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

@Api(tags = "账单-我的账单模块")
@RestController
@RequestMapping(value = "/bill/account/")
public class AccountController {

    private final IAccountService  iAccountService;

    @Autowired
    public AccountController(IAccountService iAccountService) {
        this.iAccountService = iAccountService;
    }

    @ApiOperation(value = "添加账单")
    @PostMapping(value = "add")
    public ServerResponse<String> add(@Valid AddAccountFrom addAccountFrom, BindingResult result){
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        Account account = new Account();
        BeanUtils.copyProperties(addAccountFrom,account);
        account.setCreateTime(new Date(addAccountFrom.getCreateTime()));
        int currentUserId = CurrentUser.getCurrentUserId();
        account.setFkUserId(currentUserId);
        return iAccountService.add(account);
    }

    @ApiOperation(value = "添加账单2(自定义价格)")
    @PostMapping(value = "addNotGoods")
    public ServerResponse<String> addNotGoods(@Valid AddAccountNotGoodsFrom addAccountNotGoodsFrom, BindingResult result){
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        Account account = new Account();
        BeanUtils.copyProperties(addAccountNotGoodsFrom,account);
        account.setCreateTime(new Date(addAccountNotGoodsFrom.getCreateTime()));
        int currentUserId = CurrentUser.getCurrentUserId();
        account.setFkUserId(currentUserId);
        return iAccountService.addNotGoods(account);
    }

    @ApiOperation(value = "删除账单")
    @PostMapping(value = "delete")
    public ServerResponse<String> delete(Integer id){
        if(id == null || id <= 0){
            return ServerResponse.createByErrorMessage("待删除的id不能为空或者小于0");
        }
        int currentUserId = CurrentUser.getCurrentUserId();
        return iAccountService.delete(currentUserId,id);
    }

    @ApiOperation(value = "编辑账单2(自定义价格)")
    @PostMapping(value = "editNotGoods")
    public ServerResponse<String> editNotGoods(@Valid EditAccountNotGoodsFrom editAccountNotGoodsFrom, BindingResult result){
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        Account account = new Account();
        BeanUtils.copyProperties(editAccountNotGoodsFrom,account);
        account.setCreateTime(new Date(editAccountNotGoodsFrom.getCreateTime()));
        int currentUserId = CurrentUser.getCurrentUserId();
        account.setFkUserId(currentUserId);
        return iAccountService.editNotGoods(account);
    }


    @ApiOperation("获取基本信息：总额，总付款，总未付款，个数")
    @PostMapping(value = "getStatisticalInfo")
    public ServerResponse<Map<String,Object>> getStatisticalInfo(@RequestParam(value = "timestamps[0]") Long start,
                                                                 @RequestParam(value = "timestamps[1]") Long end){
        return iAccountService.getStatisticalInfo(start,end,CurrentUser.getCurrentUserId());
    }

    @ApiOperation(value = "获取搜索时间范围内账单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前的页数",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",defaultValue = "10")
    })
    @PostMapping(value = "getAccounts")
    public ServerResponse<PageInfo<AccountVo>> getAccountListBySearchTime(@RequestParam(value = "timestamps[0]") Long start,
                                                                          @RequestParam(value = "timestamps[1]") Long end,
                                                                          @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                                          @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return iAccountService.getAccountListBySearchTime(start,end,pageNum,pageSize,CurrentUser.getCurrentUserId());
    }


    /**
     * 获取搜索时间范围内的账单趋势信息
     * @param start：搜索开始时间戳
     * @param end：搜索结束时间戳
     * @param totalPriceData ：是否查看总价趋势；默认为查看价格趋势图，false表示查找加工零件趋势
     * @return ：
     */
    @ApiOperation(value = "获取搜索时间范围内的账单趋势信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "start",value = "搜索开始时间戳"),
            @ApiImplicitParam(name = "end",value = "搜索结束时间戳"),
            @ApiImplicitParam(name = "totalPriceData",value = "是否查看总价趋势")
    })
    @PostMapping(value = "getLineData")
    public ServerResponse<LineChartVo> getLineData(@RequestParam(value = "timestamps[0]") Long start,
                                                   @RequestParam(value = "timestamps[1]") Long end,
                                                   @RequestParam(value = "totalPriceData",defaultValue = "true") Boolean totalPriceData){
        return iAccountService.getLineData(start, end, CurrentUser.getCurrentUserId(),totalPriceData);
    }

    @ApiOperation(value = "更新选中的账单的为已结算")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "以逗号分割的id字符串"),
            @ApiImplicitParam(name = "paymentTime",value = "结算时间"),
    })
    @PostMapping(value = "batchUpdateStatus")
    public ServerResponse<String> batchUpdateStatus(String ids,Long paymentTime){
        if(StringUtils.isBlank(ids)){
            return ServerResponse.createByErrorMessage("请选中待更新的账单");
        }
        return iAccountService.batchUpdateStatus(ids,paymentTime);
    }

    @ApiOperation(value = "更新搜索时间范围内的账单为已支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "start",value = "搜索开始时间戳"),
            @ApiImplicitParam(name = "end",value = "搜索结束时间戳"),
            @ApiImplicitParam(name = "paymentTime",value = "支付时间")
    })
    @PostMapping(value = "batchUpdateStatusBySearchTime")
    public ServerResponse<String> batchUpdateStatus(@RequestParam(value = "timestamps[0]") Long start,
                                                    @RequestParam(value = "timestamps[1]") Long end,
                                                    @RequestParam(value = "paymentTime") Long paymentTime) {
        return iAccountService.batchUpdateStatus(start,end,paymentTime,CurrentUser.getCurrentUserId());
    }


}
